package com.ruoyi.system.service.impl;


import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.core.page.TableSupport;
import com.ruoyi.system.domain.CashHistory;
import com.ruoyi.system.mapper.CashHistoryMapper;
import com.ruoyi.system.service.BaseServiceImpl;
import com.ruoyi.system.service.CashHistoryService;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service
public class CashHistoryServiceImpl extends BaseServiceImpl<CashHistoryMapper, CashHistory> implements CashHistoryService {
    @Override
    public List<Long> selectUserIds(Date start, Date end) {
        QueryWrapper<CashHistory> queryWrapper = new QueryWrapper<>();
        queryWrapper.ge("create_time", start);
        queryWrapper.lt("create_time", end);
        queryWrapper.select("user_id");
        queryWrapper.groupBy("user_id");
        List<CashHistory> cashHistories = this.baseMapper.selectList(queryWrapper);
        if (CollectionUtil.isEmpty(cashHistories)) {
            return null;
        }
        List<Long> userIds = cashHistories.stream().filter(p -> p != null).map(p -> p.getUserId()).collect(Collectors.toList());
        return userIds;
    }

    @Override
    public BigDecimal sum(CashHistory cashHistory) {
        QueryWrapper queryWrapper = new QueryWrapper(cashHistory);
        queryWrapper.select("sum(change_money) as changeMoney");
        queryWrapper.between("create_time", DateUtil.beginOfDay(new Date()), new Date());
        cashHistory = this.baseMapper.selectOne(queryWrapper);
        return cashHistory == null ? BigDecimal.ZERO : cashHistory.getChangeMoney().setScale(2, BigDecimal.ROUND_HALF_UP);

    }

    public List<CashHistory> findList(CashHistory t) {
        String sortName;
        // 判断对象是否有id属性
        boolean hasId = hasIdField(t);
        if (hasId) {
            sortName = "id";
        } else {
            sortName = "create_time";
        }
        String orderNo = null;
        if (StrUtil.isNotBlank(t.getOrderNo())) {
            orderNo = t.getOrderNo();
            t.setOrderNo(null);
        }

        QueryWrapper<CashHistory> query = new QueryWrapper(t);
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Map<String, Object> params = t.getParams();
        if (params != null) {
            query.between(!ObjectUtils.isEmpty(params.get("beginTime")), "create_time", params.get("beginTime") + " 00:00:00", params.get("endTime") + " 23:59:59");
        }
        query.like(StrUtil.isNotBlank(orderNo), "order_no", orderNo);
        IPage<T> page = new Page<T>();
        page.setCurrent(pageDomain.getPageNum());
        page.setSize(pageDomain.getPageSize());
        String orderByColumn = pageDomain.getOrderByColumn();
        String isAsc = pageDomain.getIsAsc();

        if (StrUtil.isBlank(orderByColumn)) {
            orderByColumn = sortName;
        }
        if (StrUtil.isBlank(isAsc)) {
            isAsc = "desc";
        }
        if ("desc".equals(isAsc)) {
            query.orderByDesc(StrUtil.toUnderlineCase(orderByColumn));
        } else {
            query.orderByAsc(StrUtil.toUnderlineCase(orderByColumn));
        }
        PageHelper.startPage(pageDomain.getPageNum(), pageDomain.getPageSize());
        List<CashHistory> records = this.baseMapper.selectList(query);
        return records;
    }
}
